package com.example.demo.componet;

import com.example.demo.model.SeansonFour;
import scala.math.Ordering;

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 读csv
 * 读取csv的某一列
 * @author xwh
 * @date 2023/02/01
 */
public class ReadCSV {

    BufferedReader br=null;
    String line = null;

    public ReadCSV(){}

    //获取某一列返回数组
    public List<String> getColumnList(String path,int column){
        ArrayList arrayList = new ArrayList();

        try {
            br = new BufferedReader(new FileReader(path));


            while ((line = br.readLine()) != null) {
                //","分割每行第6个
                //System.out.println(line.split(",")[column]);
                String a= line.split(",")[column];
                if(a !=null && a !=""){arrayList.add(a);}
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return arrayList;
    }


    /**
     * 读取时间和价格
     */
    public List<ArrayList> readYearPrice(String path,int column1,int column2){

        List<ArrayList> list = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(path));

            br.readLine();
            while ((line = br.readLine()) != null) {
                //","取出月份和价格
                //System.out.println(line.split(",")[column]);
                String month= (line.split(",")[column1]).substring(6,7);    //1
                Float price = Float.parseFloat(line.split(",")[column2]);   //7

                if(month !=null && month !=""){
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(month);
                    arrayList.add(price);
                    list.add(arrayList);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public List<ArrayList> readTwoColumn(String path,int column1,int column2){
        List<ArrayList> list = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(path));

            br.readLine();
            while ((line = br.readLine()) != null) {
                //","取出任意两列
                //System.out.println(line.split(",")[column]);
                String columnOne= line.split(",")[column1] ;   //取column1列
                String columnTwo = line.split(",")[column2];   // 。。。

                if(columnOne != "" && columnTwo !=""){
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(columnOne);
                    arrayList.add(columnTwo);
                    list.add(arrayList);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public List<ArrayList> readThreeColumn(String path,int column1,int column2,int column3){
        List<ArrayList> list = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(path));

            br.readLine();  //先读一行除去表头
            while ((line = br.readLine()) != null) {
                //","取出任意两列
                //System.out.println(line.split(",")[column]);
                String columnOne= line.split(",")[column1].substring(6,7) ;   //取column1列
                String columnTwo = line.split(",")[column2];   // 。。。
                String columnThree = line.split(",")[column3];  //

                if(columnOne != "" && columnTwo !=""){
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(columnOne);
                    arrayList.add(columnTwo);
                    arrayList.add(columnThree);
                    list.add(arrayList);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    /**
     * 读取时间和价格
     */
    public List<ArrayList> readYearAnyColumn(String path,int column){

        List<ArrayList> list = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(path));

            br.readLine();
            while ((line = br.readLine()) != null) {
                //System.out.println(line.split(",")[column]);
                String month= (line.split(",")[1]).substring(6,7);    //1
                String element = (String) line.split(",")[column];   //7

                if(month !=null && month !=""){
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(month);
                    arrayList.add(element);
                    list.add(arrayList);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }



    public List<SeansonFour> readThreeColumn2Bean(String path, int column1, int column2, int column3){
        List<SeansonFour> list = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(path));

            br.readLine();  //先读一行除去表头
            while ((line = br.readLine()) != null) {
                //","取出任意两列
                //System.out.println(line.split(",")[column]);
                String columnOne= line.split(",")[column1].substring(5,7) ;   //取column1列
                //System.out.println(columnOne);
                String columnTwo =line.split(",")[column2] ;   // 。。。
                //System.out.println(columnTwo);
                String columnThree = line.split(",")[column3];  //
                //System.out.println(columnThree);

                if(columnOne != "" && columnOne != null){
                    SeansonFour seansonFour = new SeansonFour();
                    seansonFour.setEventTime(columnOne);
                    seansonFour.setPrice(columnTwo);
                    seansonFour.setLocal(columnThree);
                    list.add(seansonFour);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    /*
    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\fireapproval\\Desktop\\毕业设计\\数据集\\test.csv"));

        String line;

        while ((line = br.readLine()) != null) {
            //","分割每行第6个
            System.out.println(line.split(",")[6]);

        }

        br.close();
    }

     */


}